package com.hxgis.statisticanalysis.intelligentIndicator.controller;

import com.hxgis.statisticanalysis.intelligentIndicator.vo.CompletedWorkOrderStatistic;
import com.hxgis.statisticanalysis.intelligentIndicator.pojo.WorkOrderInfo;
import com.hxgis.statisticanalysis.intelligentIndicator.vo.WorkOrderQuery;
import com.hxgis.statisticanalysis.intelligentIndicator.vo.WorkOrderStatistic;
import com.hxgis.statisticanalysis.intelligentIndicator.service.WorkOrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 赵恒
 * @date 2022/12/13
 */
@RestController
@RequestMapping(("/statisticanalysis/frontapi/v1"))
@Api(tags = {"工单信息"})
@Slf4j
public class WorkOrderInfoController {
    @Autowired
    WorkOrderInfoService workOrderInfoService;

//    @RequestMapping(value = "/workOrder/getAll", method = RequestMethod.GET)
//    @ApiOperation(value = "获取所有的工单信息")
    public List<WorkOrderInfo> getAll() {
        return workOrderInfoService.list();
    }

//    @RequestMapping(value = "/workOrder/query", method = RequestMethod.POST)
//    @ApiOperation(value = "按照条件获取工单信息")
    public List<WorkOrderInfo> query(@RequestBody @Validated WorkOrderQuery workOrderQuery) {
        return workOrderInfoService.query(workOrderQuery);
    }

    @RequestMapping(value = "/workOrder/updateByMonth", method = RequestMethod.POST)
    @ApiOperation(value = "刷新历史工单信息")
    public String updateByMonth(String startMonth, String endMonth) {
        boolean b = workOrderInfoService.updateByMonth(startMonth, endMonth);
        if (b) {
            return "更新成功";
        } else {
            throw new RuntimeException("更新失败");
        }
    }

    @RequestMapping(value = "/workOrder/getWorkOrderStatistic", method = RequestMethod.GET)
    @ApiOperation(value = "获取气象服务流程工单统计表")
    public List<WorkOrderStatistic> getWorkOrderStatistic(String startMonth, String endMonth) {
        List<WorkOrderInfo> workOrderInfoList = workOrderInfoService.getWorkOrderInfoByMonth(startMonth, endMonth);
        return workOrderInfoService.getWorkOrderStatistic(workOrderInfoList);
    }

    @RequestMapping(value = "/workOrder/getCompletedWorkOrderStatistic", method = RequestMethod.GET)
    @ApiOperation(value = "获取气象服务流程已解决工单统计表")
    public List<CompletedWorkOrderStatistic> getCompletedWorkOrderStatistic(String startMonth, String endMonth) {
        List<WorkOrderInfo> workOrderInfoList = workOrderInfoService.getWorkOrderInfoByMonth(startMonth, endMonth);
        return workOrderInfoService.getCompletedWorkOrderStatistic(workOrderInfoList);
    }

    @RequestMapping(value = "/workOrder/exportWord", method = RequestMethod.GET)
    @ApiOperation(value = "导出word文档")
    public void exportWord(String startMonth, String endMonth, HttpServletResponse response) {
        if (startMonth == null || endMonth == null) {
            throw new RuntimeException("时间不能为空");
        }
        workOrderInfoService.exportWord(startMonth, endMonth, response);
    }

    @RequestMapping(value = "/workOrder/update", method = RequestMethod.POST)
    @ApiOperation(value = "批量修改工单")
    public String updateWorkOrder(@RequestBody List<WorkOrderInfo> workOrderInfoList) {
        boolean b = workOrderInfoService.saveOrUpdateBatch(workOrderInfoList);
        if (b) {
            return "修改成功";
        } else {
            throw new RuntimeException("修改失败");
        }
    }
}
